package com.ailk.tcm.hffw.android.utils;

import java.util.List;

import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ailk.tcm.hffw.android.common.ui.DWAdapter;
import com.ailk.tcm.hffw.android.common.ui.FooterView;

/**
 * 构造分页listview的工具类
 * @author chris
 *
 * @since 2012-9-24 下午5:24:44
 */
public class PageListViewBuilder {
	
	public static final int PAGE_SIZE = 10;
	
	/**
	 * 构造分页listview，并自动加载第一页数据
	 * @author chris
	 * @param listView
	 * @param adapter
	 * @param loader
	 * @param pageSize
	 * @return
	 * @since 2012-9-24 下午6:18:28
	 */
	public static <T> PageListViewController<T> buildAndRefresh(final ListView listView, final DWAdapter<T> adapter, 
			final DataLoader<T> loader){
		PageListViewController<T> pc = build(listView, adapter, loader);
		pc.refresh();
		return pc;
	}
	
	/**
	 * 构造分页listview
	 * @author chris
	 * @param listView
	 * @param adapter
	 * @param loader
	 * @param callback
	 * @param pageSize
	 * @return
	 * @since 2012-9-24 下午6:18:33
	 */
	public static <T> PageListViewController<T> build(final ListView listView, final DWAdapter<T> adapter, 
			final DataLoader<T> loader){
		listView.setAdapter(adapter);
		final LinearLayout loadFooter = new FooterView(listView.getContext());
		listView.addFooterView(loadFooter);
		listView.setOnScrollListener(new OnScrollListener() {
			private int lastItem;
			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				if(lastItem == adapter.getCount() - 1 && !adapter.hasLoadAllDatas()){
					try {
						loader.load(lastItem/PAGE_SIZE + 1, new DataLoader.ActionListener<T>() {
							@Override
							public void onResponse(List<T> data) {
								adapter.loadList(PAGE_SIZE, data);
								if(adapter.hasLoadAllDatas()){
									listView.removeFooterView(loadFooter);
								}
								loader.afterLoad(data, adapter);
							}
						});
					} catch (Exception e) {
					}
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, 
					int visibleItemCount, int totalItemCount) {
				lastItem = firstVisibleItem + visibleItemCount - 2;
			}
		});
		listView.setAdapter(adapter);
		return new PageListViewController<T>(listView, adapter, loader, loadFooter);
	}
	
	public static interface DataLoader<T>{
		public void load(int page, ActionListener<T> listener) throws Exception;
		/**
		 * 加载完一页数据后回调方法
		 * @author chris
		 * @param pageDatas		已加载的一页数据
		 * @param adapter
		 * @since 2012-12-22 下午4:10:38
		 */
		public void afterLoad(List<T> pageDatas, DWAdapter<T> adapter);
		
		interface ActionListener<T>{
			public void onResponse(List<T> data);
		}
	}
	
	public static class PageListViewController<T>{
		private ListView listView;
		private DWAdapter<T> adapter;
		private DataLoader<T> loader;
		private LinearLayout loadFooter;
		
		private PageListViewController(final ListView listView, final DWAdapter<T> adapter, 
				final DataLoader<T> loader, final LinearLayout loadFooter){
			this.listView = listView;
			this.adapter = adapter;
			this.loader = loader;
			this.loadFooter = loadFooter;
		}
		
		public void refresh(){
			adapter.removeAll();
			if(listView.getFooterViewsCount() == 0){
				listView.addFooterView(loadFooter);
			}
			try {
				loader.load(0, new DataLoader.ActionListener<T>() {
					@Override
					public void onResponse(List<T> data) {
						adapter.loadList(PAGE_SIZE, data);
						if(adapter.hasLoadAllDatas()){
							listView.removeFooterView(loadFooter);
						}
						loader.afterLoad(data, adapter);
					}
				});
			} catch (Exception e) {
			}
		}
	}
}
